package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.entity.Budget;
import pers.cj.pdos.mapper.BudgetMapper;
import pers.cj.pdos.mapper.OrderRuleMapper;
import pers.cj.pdos.mapper.SettAccountMapper;
import pers.cj.pdos.service.IBudgetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.BudgetVo;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class BudgetServiceImpl extends ServiceImpl<BudgetMapper, Budget> implements IBudgetService {
    @Autowired
    OrderRuleMapper orderRuleMapper;
    @Autowired
    BudgetMapper budgetMapper;
    @Autowired
    SettAccountMapper settAccountMapper;

    @Override
    public List<BudgetVo> listVoPage(String companyId,
                                     Integer page,
                                     Integer limit,
                                     Integer type,
                                     Boolean close,
                                     String search,
                                     Date makeTime1,
                                     Date makeTime2,
                                     Date date1,
                                     Date date2,
                                     String makeUserId,
                                     String storeId,
                                     String budgetProjectId,
                                     String unit,
                                     String settAccountId,
                                     String userStoreId,
                                     String userId,
                                     String remark) {
        return budgetMapper.listVo(companyId,
                Utils.genPage(page,limit),
                limit,
                type,
                close,
                Utils.genLikeData(search),
                makeTime1,
                makeTime2,
                date1,
                date2,
                makeUserId,
                storeId,
                budgetProjectId,
                unit,
                settAccountId,
                userStoreId,
                userId,
                remark);
    }

    @Override
    public long count(String companyId,
                      Integer type,
                      Boolean close,
                      String search,
                      Date makeTime1,
                      Date makeTime2,
                      Date date1,
                      Date date2,
                      String makeUserId,
                      String storeId,
                      String budgetProjectId,
                      String unit,
                      String settAccountId,
                      String userStoreId,
                      String userId,
                      String remark) {
        return budgetMapper.count(companyId,
                type,
                close,
                Utils.genLikeData(search),
                makeTime1,
                makeTime2,
                date1,
                date2,
                makeUserId,
                storeId,
                budgetProjectId,
                unit,
                settAccountId,
                userStoreId,
                userId,
                remark);
    }

    @Override
    public BudgetVo getVoById(String id) {
        return budgetMapper.getVoById(id);
    }

    @Override
    public List<BudgetVo> listVoByIds(List<String> ids) {
        return budgetMapper.listVoByIds(ids);
    }

    @Override
    public boolean checkClose(Budget budget) {
        return budgetMapper.selectCount(new QueryWrapper<Budget>().eq("id",budget.getId()).eq("close",true)) > 0;
    }

    @Override
    public void insert(Budget budget) {
        budgetMapper.insert(budget);

        if (budget.getType()==1){
            //收入 余额+
            settAccountMapper.addBalance(budget.getAmt(), budget.getSettAccountId());
        } else {
            //支出 余额-
            settAccountMapper.addBalance(budget.getAmt().negate(), budget.getSettAccountId());
        }
    }

    @Override
    public void close(Budget budget) {

        Budget temp = new Budget();
        temp.setId(budget.getId());
        temp.setClose(true);
        budgetMapper.updateById(temp);

        Budget db = budgetMapper.selectById(budget.getId());
        if (db.getType()==1){
            //收入 余额-
            settAccountMapper.addBalance(db.getAmt().negate(), db.getSettAccountId());
        } else {
            //支出 余额+
            settAccountMapper.addBalance(db.getAmt(), db.getSettAccountId());
        }

    }
}
